package br.uvv.Util;

import java.io.IOException;
import java.text.DecimalFormat;

import javax.faces.FacesException;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import br.uvv.Model.Usuario;
import br.uvv.Web.ManagedBean.ContextoBean;

public class Util {
	
	public static String formataMoedaReal(Double vlr){
		DecimalFormat df = new DecimalFormat("###,###,##0.00");
        String dx = df.format(vlr);
		return dx;
	} 
	
	public static String gerarSenha(){
		String senha = "";
		int random;
		boolean v = false;

		for(int i = 0; i<8; i++) {
			v = false;
		
			do {
				random = (int) (Math.random() * 122);
				
				if (random >= 48 && random <= 57)
					v = true;
				else if (random >= 97 && random <= 122)
					v = true;
				else if (random >= 65 && random <= 90)
					v = true;
			} while (!v);
			
			senha += ((char) random);
		}
		return senha;
	}
	
	public static String gerarLinkAtivacao(){
		FacesContext context = FacesContext.getCurrentInstance();  
		HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();  
		String urlAtual = request.getRequestURL().toString();
		String arrayUrl[];
		arrayUrl=urlAtual.split("/");
		String novaUrl="";
		for(int i=0; i<arrayUrl.length; i++){
			if(i<=5){
				if(i==0){ novaUrl = novaUrl+ arrayUrl[i]; }
				if(i==1){ novaUrl = novaUrl+"//"+ arrayUrl[i]; }
				if(i>1){ novaUrl = novaUrl + arrayUrl[i] + "/";}
			}
		}
		novaUrl=novaUrl+"confirmacaoEmail.jsf?id=";
		return novaUrl;
	}

	public static void redirecionar(String destino){
		FacesContext ctx = FacesContext.getCurrentInstance();
        ExternalContext extContext = ctx.getExternalContext();
        String url = extContext.encodeActionURL(ctx.getApplication().getViewHandler().getActionURL(ctx, destino));
        try {
            extContext.redirect(url);
        } catch (IOException ioe) {
            throw new FacesException(ioe);
		}
	}
	
	public static Object capturaBean(String nomeBean){
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	    HttpSession session = ((HttpServletRequest)request).getSession();
	    return session.getAttribute(nomeBean);
	}
	
	public static Usuario getUsuarioLogado(){
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
	    HttpSession session = ((HttpServletRequest)request).getSession();
		try {
			ContextoBean bean = (ContextoBean) session.getAttribute("contextoBean");
		    if(bean!=null){
		    	Usuario usuario = bean.getUsuario();
		    	if(usuario != null && usuario.getId() != null){
		    		return usuario;
		    	}
		    }
		} catch (NullPointerException e) {}
	    return null;
	}
	
	public static void setDataTableRows(Integer rows){
		ContextoBean bean = (ContextoBean) capturaBean("contextoBean");
		if (bean != null)
			bean.setDataTableRows(rows);
	}
}